package com.medical.data.controller;

import com.medical.data.component.TemplateTypeComponent;
import com.medical.data.domain.FormItem;
import com.medical.data.domain.FormTemplateType;
import com.medical.data.service.FormItemService;
import com.medical.data.utils.Result;
import com.medical.data.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/front/formItem")
@Api(description = "表单组件管理")
@Slf4j
public class FormItemController {

    @Autowired
    private FormItemService formItemService;

    @ApiOperation("更新表单组件")
    @PostMapping("/updateFormItem")
    public Result updateFormItem(@Validated @RequestBody AddFormItemVo addFormItemVo) {
        log.info("front/formItem/updateFormItem参数========>{}", addFormItemVo);
        formItemService.updateFormItem(addFormItemVo);
        return Result.ok();
        //"formTemplateId": "7f3a220a9a55b13832b0657ff4ad8da2"
    }

    @ApiOperation("表单组件排序")
    @PostMapping("/formItemSort")
    public Result formItemSort(@RequestBody FormItemSortVo formItemSortVo) {
        log.info("front/formItem/formItemSort参数========>{}", formItemSortVo);
        AddFormItemResultVo addFormItemResultVo = formItemService.formItemSort(formItemSortVo);
        if (ObjectUtils.isEmpty(addFormItemResultVo)) {
            return Result.ok();
        }
        return Result.ok().data("data", addFormItemResultVo);
        //"formTemplateId": "7f3a220a9a55b13832b0657ff4ad8da2"
    }

    @ApiOperation("删除组件")
    @PostMapping("/deleteFormItem/{formId}/{formItemId}")
    public Result deleteFormItem(@PathVariable("formId") String formId, @PathVariable("formItemId") String formItemId) {
        log.info("front/formItem/deleteFormItem参数========>{}", formId, formItemId);
        formItemService.deleteFormItem(formId, formItemId);
        return Result.ok();
    }

    @ApiOperation("预览")
    @PostMapping("/previewFormItem/{formId}")
    public Result previewFormItem(@PathVariable("formId") String formId) {
        log.info("front/formItem/previewFormItem参数========>{}", formId);
        PreviewFormItemVo previewFormItemVo = formItemService.previewFormItem(formId);
        return Result.ok().data("previewFormItemVo", previewFormItemVo);
    }

    @ApiOperation("创建或者更新所有组件")
    @PostMapping("/createAllFormItem")
    public Result createAllFormItem(@RequestBody FormItem formItem) {
        log.info("front/formItem/createAllFormItem参数========>{}",formItem);
        formItemService.addAllFormItem(formItem);
        return Result.ok();
    }

    @ApiOperation("获取所有组件")
    @PostMapping("/getAllFormItem/{formId}")
    public Result getAllFormItem(@PathVariable("formId") String formId) {
        log.info("front/formItem/getAllFormItem参数========>{}", formId);
        FormItem formItem=formItemService.getAllFormItem(formId);
        return Result.ok().data("formItem", formItem);
    }

    @ApiOperation("获取所有组件")
    @PostMapping("/getAllFormItemByFormId/{formId}")
    public Result getAllFormItemByFormId(@PathVariable("formId") String formId) {
        log.info("front/formItem/getAllFormItemByFormId参数========>{}", formId);
        Map<String,String> map=formItemService.getAllFormItemByFormId(formId);
        return Result.ok().data("formItem", map);
    }

}
